{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "\n",
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = '1'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Dataset\n",
    "## The dataset may be 'cora', 'citeseer', 'pubmed'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from graphgallery.datasets import Planetoid\n",
    "data = Planetoid('cora', root=\"~/GraphData/datasets/\", verbose=False)\n",
    "graph = data.graph\n",
    "splits = data.split_nodes()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Init"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "table = []\n",
    "device = \"gpu\"\n",
    "runs = 10\n",
    "\n",
    "GNNs = (\"GCN\", \"SGC\", \"GAT\")\n",
    "Epochs = (200, 100, 200)\n",
    "Backends = (\"TensorFlow\", \"PyTorch\", \"PyG\", \"DGL\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Benchmarking Experiments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GCN(device=GPU:0, backend=TensorFlow 2.1.2 Backend): \u001b[31m80.48%±0.97%\u001b[0m\n",
      "SGC(device=GPU:0, backend=TensorFlow 2.1.2 Backend): \u001b[31m81.33%±0.61%\u001b[0m\n",
      "WARNING:tensorflow:Large dropout rate: 0.6 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n",
      "WARNING:tensorflow:Large dropout rate: 0.6 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n",
      "WARNING:tensorflow:Large dropout rate: 0.6 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n",
      "WARNING:tensorflow:Large dropout rate: 0.6 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n",
      "WARNING:tensorflow:Large dropout rate: 0.6 (>0.5). In TensorFlow 2.x, dropout() uses dropout rate instead of keep_prob. Please ensure that this is intended.\n",
      "GAT(device=GPU:0, backend=TensorFlow 2.1.2 Backend): \u001b[31m81.96%±1.02%\u001b[0m\n",
      "GCN(device=cuda:0, backend=PyTorch 1.6.0+cu101 Backend): \u001b[31m81.25%±0.88%\u001b[0m\n",
      "SGC(device=cuda:0, backend=PyTorch 1.6.0+cu101 Backend): \u001b[31m80.53%±0.48%\u001b[0m\n",
      "GAT(device=cuda:0, backend=PyTorch 1.6.0+cu101 Backend): \u001b[31m82.74%±0.58%\u001b[0m\n",
      "GCN(device=cuda:0, backend=PyTorch Geometric 1.6.1 (PyTorch 1.6.0+cu101) Backend): \u001b[31m81.22%±0.75%\u001b[0m\n",
      "SGC(device=cuda:0, backend=PyTorch Geometric 1.6.1 (PyTorch 1.6.0+cu101) Backend): \u001b[31m80.77%±0.58%\u001b[0m\n",
      "GAT(device=cuda:0, backend=PyTorch Geometric 1.6.1 (PyTorch 1.6.0+cu101) Backend): \u001b[31m82.72%±1.08%\u001b[0m\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using backend: pytorch\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GCN(device=cuda:0, backend=DGL PyTorch 0.5.2 (PyTorch 1.6.0+cu101) Backend): \u001b[31m81.39%±0.44%\u001b[0m\n",
      "SGC(device=cuda:0, backend=DGL PyTorch 0.5.2 (PyTorch 1.6.0+cu101) Backend): \u001b[31m80.70%±0.54%\u001b[0m\n",
      "GAT(device=cuda:0, backend=DGL PyTorch 0.5.2 (PyTorch 1.6.0+cu101) Backend): \u001b[31m82.18%±0.47%\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "from graphgallery import gallery\n",
    "from graphgallery import set_backend\n",
    "\n",
    "for backend in Backends:\n",
    "    set_backend(backend)\n",
    "    tmp = []\n",
    "    for Net, epochs in zip(GNNs, Epochs):\n",
    "        reports = []\n",
    "        for seed in range(runs):\n",
    "            trainer = getattr(gallery, Net)(graph, device=device, seed=42+seed).process(attr_transform=\"normalize_attr\").build()\n",
    "            his = trainer.train(splits.train_nodes, splits.val_nodes, verbose=0, epochs=epochs)\n",
    "            results = trainer.test(splits.test_nodes, verbose=0) \n",
    "            reports.append(results.accuracy)\n",
    "\n",
    "        reports = f\"{np.mean(reports):.2%}±{np.std(reports):.2%}\"\n",
    "        print(f\"{trainer}: \\033[31m{reports}\\033[0m\")        \n",
    "        tmp.append(reports)\n",
    "    table.append(tmp)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Results on Cora Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>GCN</th>\n",
       "      <th>SGC</th>\n",
       "      <th>GAT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>TensorFlow</th>\n",
       "      <td>80.48%±0.97%</td>\n",
       "      <td>81.33%±0.61%</td>\n",
       "      <td>81.96%±1.02%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PyTorch</th>\n",
       "      <td>81.25%±0.88%</td>\n",
       "      <td>80.53%±0.48%</td>\n",
       "      <td>82.74%±0.58%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>PyG</th>\n",
       "      <td>81.22%±0.75%</td>\n",
       "      <td>80.77%±0.58%</td>\n",
       "      <td>82.72%±1.08%</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DGL</th>\n",
       "      <td>81.39%±0.44%</td>\n",
       "      <td>80.70%±0.54%</td>\n",
       "      <td>82.18%±0.47%</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     GCN           SGC           GAT\n",
       "TensorFlow  80.48%±0.97%  81.33%±0.61%  81.96%±1.02%\n",
       "PyTorch     81.25%±0.88%  80.53%±0.48%  82.74%±0.58%\n",
       "PyG         81.22%±0.75%  80.77%±0.58%  82.72%±1.08%\n",
       "DGL         81.39%±0.44%  80.70%±0.54%  82.18%±0.47%"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "table = pd.DataFrame(table, columns=GNNs)\n",
    "table.index = Backends\n",
    "table"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
